package demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2023-11-29
 * Time: 0:11
 */


//生成一副扑克牌,52张
    //4花色,13种不同数字
public class CardGame {
    public static final String[] suits = {"♥","♣","♦","♠"};//final表示这个变量的值一旦被初始化就不能被改变
    public List<Card> buyCard(){      //这是一个方法,List<Card>是一个泛型列表，用于存储Card对象。
        List<Card> cardList = new ArrayList<>();//创建一维数组可以储存"Card"的ArrayList实例,就是一副牌
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13 ; j++) {
                String suit = suits[i]; //花色数组
                Card card = new Card(suit,j);//花色和数字构成一张牌实例
                cardList.add(card);//每循环一次得到一张牌就放到集合里
            }
        }
        return cardList;//放好了52张牌
    }

    //洗牌
    public void shuffle(List<Card> cardList){ //传一副牌过来
        Random random = new Random();
        for (int i = cardList.size()-1; i > 0  ; i--) {
            int index = random.nextInt(i);
            swap(cardList,i,index);
        }
    }

    private static void swap(List<Card> cardList,int i,int j){
        Card tmp = cardList.get(i);
        cardList.set(i,cardList.get(j));
        cardList.set(j,tmp);
    }

    //3个人轮流抓5张牌
    public List<List<Card>> getCard(List<Card> cardList){
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();
        List<List<Card>> hand= new ArrayList<>(); //二维数组
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //怎么揭牌?每次相当于删掉0 下标这个牌
                Card card = cardList.remove(0);
                //放到对应人的手里
                hand.get(j).add(card);//相当于hand1,2,3.add(card)

            }
        }
        return hand;
    }
}
